<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MasterPage.Master" Inherits="System.Web.Mvc.ViewPage<Animoso.Domain.Administration>" %>
<%@ Import Namespace="Animoso.Domain" %>
<%@ Import Namespace="Animoso.Web.Controllers" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Edit
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="HeadPlaceHolder" runat="server">
    <link href="../../uploadify/uploadify.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
    $(document).ready(function() {
        $('#upload').uploadify({
            'uploader': '/uploadify/uploadify.swf',
            'cancelImg': '/uploadify/cancel.png',
            'script': '/ManageAdministration/Upload',
            'onComplete': handleResponse,
            'scriptData': { administrationId: '<%=Model.Id %>' }
        });
    });

    function handleResponse(vent, queueID, fileObj, response, data) {
        $('#fotoPreview').attr('src', '/<%=Folders.ADMINISTRATION_PICTURE_FOLDER %>/' + response.toString());
    }
</script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Bestuur</h2>

<%
        IEnumerable<Role> rolesEnum = Enum.GetValues(typeof(Role)).Cast<Role>();
        
       string roles = "";
        foreach (Role role in rolesEnum)
{
    roles += "<option value=\"" + role + "\">" + role + "</option>";
}
        IEnumerable<Commission> commissionsObjects = (IEnumerable<Commission>) ViewData["Commissions"];
        string commissions = "";
        foreach (Commission commission in commissionsObjects)
{
  commissions += "<option value=\"" + commission.Id + "\">" + commission.Name + "</option>";
}
        %>
    <%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>

    <% using (Html.BeginForm("Edit", "ManageAdministration", FormMethod.Post)) {%>

        <fieldset>
            <legend>Fields</legend>
            <p>
                <label for="Picture">Foto:</label><br />
                <input type="file" name="upload" id="upload" />
                <%=Html.ValidationMessage("Picture", "*") %>   
                <a href="javascript:$('#upload').uploadifyUpload();">Upload</a>            
                <% if (!string.IsNullOrEmpty(Model.Picture))
{%>
                    <img id="fotoPreview" src="/<%=Folders.ADMINISTRATION_PICTURE_FOLDER + "/" + Model.Picture%>" />
                    <%
}
                   else
                   {
           %>
           <img id="fotoPreview" />
           <% } %>
            </p>      
            <p>
                <label for="StartDate">StartDate:</label><br />
                <%= Html.TextBox("StartDate", String.Format("{0:g}", Model.StartDate), new { id = "startDate"}) %>
                <%= Html.ValidationMessage("StartDate", "*") %>
            </p>
            <p>
                <label for="EndDate">EndDate:</label><br />
                <%= Html.TextBox("EndDate", String.Format("{0:g}", Model.EndDate), new { id = "endDate" })%>
                <%= Html.ValidationMessage("EndDate", "*") %>
            </p>
            <p>
                <label for="Members">Leden:</label><br />                
                <a href="#" id="addMemberButton"><img src="../../Content/Images/Button-Add-icon.png" /></a><br />     
                <div id="addMember" style="visibility: hidden;">
                    <input id="firstNameText" type="text" name="firstName" /><input id="lastNameText" name="lastName" type="text" /><select id="roleSelect" name="role"><%=roles %></select><select id="commissionSelect" name="commission" disabled="disabled"><%=commissions %></select><input id="okButton" type="button" value="OK" />                    
                </div>
                
                <table id="memberTable">                    
                </table>           
            </p>
               
            <p>
                <%=Html.Hidden("Id", Model.Id) %>
                <input type="submit" value="Save" />
            </p>
        </fieldset>

    <% } %>

    <div>
        <%=Html.ActionLink("Back to List", "Index") %>
    </div>
    
<script type="text/javascript">

    $(function() {
        $("#startDate").datepicker({ dateFormat: 'dd-mm-yy' });
        $("#endDate").datepicker({ dateFormat: 'dd-mm-yy' });
    });

    $('#addMemberButton').click(function() {
        $('#addMember').css('visibility', 'visible');
    });

    $('#roleSelect').change(function() {
        var role = $('#roleSelect').val();        

        if (role == 'Chairman')            
            $('#commissionSelect').attr('disabled', false);
        else            
            $('#commissionSelect').attr('disabled', true);
    });

    $('#okButton').click(function() {
        var firstName = $('#firstNameText').val();
        var lastName = $('#lastNameText').val();
        var role = $('#roleSelect').val();
        var commissionId = $('#commissionSelect').val();

        $.ajax({ url: "/ManageAdministration/AddMember", data: ({
            administrationId: '<%=Model.Id %>',
            firstName: firstName,
            lastName: lastName,
            role: role,
            commissionId: commissionId
        }), context: document.body,
            success: function(val) {
                var roleString;
                if (val.Role == 0)
                    roleString = "Penningmeester";
                if (val.Role == 1)
                    roleString = "Voorzitter";
                if (val.Role == 2)
                    roleString = "Animoso Voorzitter";
                var baseString = '<td>' + val.FirstName + '</td><td>' + val.LastName + '</td><td>' + roleString + '</td>';
                var extraString = "";

                if (val.Commission)
                    extraString = '<td>' + val.Commission.Name + '</td>';

                $('<tr>' + baseString + extraString + '</tr>').appendTo('#memberTable');
            }
        });
    });
    </script>
</asp:Content>